package com.offcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.offcn.mapper.*;
import com.offcn.pojo.*;
import com.offcn.service.SetmealService;
import com.offcn.utils.MessageConstant;
import com.offcn.utils.PageResult;
import com.offcn.utils.QueryPageBean;

import com.offcn.utils.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: offcnpe_parent
 * @description:
 * @author: xin yi
 * @create: 2021-07-16 13:42
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageResult getAllSetmeal(QueryPageBean queryPageBean) {    /*setmeal.html页面的分页查询方法QueryPageBean对象里封装下面的几项然后整体对象作为参数传递，也就等同于前台获取的获取参数时使用的response*/
        /*1.设置分页*/                /*public Page(long current, long size) {this(current, size, 0L);}*//*包米豆自带的分页*/
        Page<Setmeal> setmealPage = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Setmeal> wrapper = null;
        /*利用QueryWrapper条件构造器判断并设置分页条件*/
        if (queryPageBean.getQueryString() != null && !"".equals(queryPageBean.getQueryString())){
            wrapper = new QueryWrapper<>();
            wrapper.eq("code",queryPageBean.getQueryString())
                    .or().like("name",queryPageBean.getQueryString())
                    .or().like("helpCode",queryPageBean.getQueryString());
        }
        /*根据selectPage条件构造器查询*//*<E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);*/
        /*需要传递一个page对象和QueryWrapper条件构造器对象*/
        Page<Setmeal> selectPage = setmealMapper.selectPage(setmealPage, wrapper);
        /*把要返回给前台的数据封装进PageResult对象  用那个response取*/
//        public PageResult(Long total, List rows) {
//            super();
//            this.total = total;
//            this.rows = rows;
//        }
        /*根据PageResult里封装的参数分别传递一个long类型的total和list集合///抽象容器*/
        PageResult pageResult = new PageResult(selectPage.getTotal(), selectPage.getRecords());
        return pageResult;
    }

    @Override
    public Result addSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {    /*setmeal.html页面的套餐添加方法，传递的参数为添加的套餐对象和放检查组id的数组*/
        /*套餐主表可以直接添加*/
        int row = setmealMapper.insert(setmeal);
        int row02 = 0;
        /*再遍历选中的检查组id添加到中间表，中间表两个id都需要setjin中间表对象中才能添加成功*/
        for (Integer id:checkgroupIds){
            /*创建一个中间表的对象*/
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(id);
            row02 += setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
        System.out.println("中间表插入的数据条数：————————" + row02 );
        if (row > 0){
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
    }

    @Override
    public Setmeal getSetmealById(Integer id) {    /*setmeal.html页面的套餐编辑方法，这里传递的是row.id也就是行id，主表id 发送请求查询该条记录的数据进行数据的回显*/
        return setmealMapper.selectById(id);

    }

    @Override
    public Result editSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {    /*setmeal.html页面的套餐的方法，在回显套餐主表数据和检查组数据之后，修改需要传递主表对象和选中的检查组id，然后在删除所有的数据之后进行数据数据的添加*/
        int row = setmealMapper.updateById(setmeal);
        /*根据检查组id删除中间表中的数据，然后重新添加*/
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("setmeal_id",setmeal.getId());
//        setmealCheckgroupMapper.deleteByMap(map);
        /*利用构造器删除中间表setmeal_id的所有数据再走添加的流程，两个id都需要添加*/
        QueryWrapper<SetmealCheckgroup> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmeal.getId());
        setmealCheckgroupMapper.delete(wrapper);
        if (checkgroupIds.length >0 && checkgroupIds != null){
            for (Integer checkgroupId : checkgroupIds) {
                SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
                setmealCheckgroup.setCheckgroupId(checkgroupId);
                setmealCheckgroup.setSetmealId(setmeal.getId());
                setmealCheckgroupMapper.insert(setmealCheckgroup);
            }
        }
        if (row > 0){
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
    }

    @Override
    public Result deleteSetmealById(Integer id) {    /*setmeal.html页面的套餐的删除方法，需要先删除中间表这里传递的id是row.id，也就是主表id*/
        /*利用querywapper条件构造器删除中间表有关setmeal_id的所有数据 也可以用hashmap的put*/
//        HashMap<String, Object> map = new HashMap<>();
        /*put添加的第一个参数为数据库字段名跟条件构造器一样，第二个为对应的对象*/
//        map.put("setmeal_id",id);
//        setmealCheckgroupMapper.deleteByMap(map);
        QueryWrapper<SetmealCheckgroup> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        setmealCheckgroupMapper.delete(wrapper);
        /*再删主表*/
        int row = setmealMapper.deleteById(id);
        if(row > 0){
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        }
        return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);

    }

    @Override
    public Result m_getAllSetmeal() {
        List<Setmeal> setmealList = setmealMapper.selectList(null);
        if (setmealList.size() >0 && setmealList != null){
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,setmealList);
        }
        return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
    }

    @Override
    public Result m_findInfoById(Integer id) {
        /*根据套餐的id查询单个套餐信息*/
        Setmeal setmeal = setmealMapper.selectById(id);

        /*查询中间表该套餐所有的id*/
        QueryWrapper<SetmealCheckgroup> setmealCheckgroupQueryWrapper = new QueryWrapper<>();
        setmealCheckgroupQueryWrapper.eq("setmeal_id",id);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(setmealCheckgroupQueryWrapper);

        List<Checkgroup> checkgroupList = new ArrayList<>();
        for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroups) {
            /*查找每个group*/
            Checkgroup checkgroup = checkgroupMapper.selectById(setmealCheckgroup.getCheckgroupId());
            /*根据检查组查询每个检查组对应的检查项id*/
            QueryWrapper<CheckgroupCheckitem> checkgroupCheckitemQueryWrapper = new QueryWrapper<>();
            checkgroupCheckitemQueryWrapper.eq("checkgroup_id",checkgroup.getId());
            List<CheckgroupCheckitem> checkgroupCheckitemList = checkgroupCheckitemMapper.selectList(checkgroupCheckitemQueryWrapper);
            List<Checkitem> checkitemList = new ArrayList<>();
            for (CheckgroupCheckitem checkgroupCheckitem : checkgroupCheckitemList) {
                /*查找每个item*/
                Checkitem checkitem = checkitemMapper.selectById(checkgroupCheckitem.getCheckitemId());
                checkitemList.add(checkitem);
            }
            checkgroup.setCheckItems(checkitemList);
            checkgroupList.add(checkgroup);
        }
        setmeal.setCheckGroups(checkgroupList);
        return new Result(true,MessageConstant.QUERY_INFO_SUCCESS,setmeal);
    }

    @Override
    public Result getSetmealReport() {
        /*放套餐名字*/
        List<String> setmeals = new ArrayList<>();
        /*放套餐数量*//*前段要的集合里面放map格式*/
        List<Map<String,Object>> setmealCount = new ArrayList<>();

        /*查询所有的套餐名字*/
        List<Setmeal> setmealList = setmealMapper.selectList(null);
        for (Setmeal setmeal : setmealList) {
            setmeals.add(setmeal.getName());
            QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.eq("setmeal_id",setmeal.getId());
            Integer count = orderMapper.selectCount(orderQueryWrapper);
            HashMap<String, Object> serMap = new HashMap<>();
            serMap.put("value",count);
            serMap.put("name",setmeal.getName());
            setmealCount.add(serMap);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames",setmeals);
        map.put("setmealCount",setmealCount);
        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
    }
}
